const jsonParse = require('../../lib/jsonParse')
const Header = require('../../lib/getHeader');
const CarStation = require('../../schemas/stations').CarStations

module.exports = {
    /**
     * 场站分页列表
     * @param {*} req 
     * @param {*} res 
     */
    async stationListsByPage(req, res) {
        let {
            pageSize,
            pageNum,
            stationName
        } = req.body;
        let data = {}
        if (stationName) {
            data.stationName = {
                $exists: stationName
            }
        }
        let total = await CarStation.find(data).count()
        CarStation.find(data).skip(pageSize * (pageNum - 1)).limit(pageSize).then(list => {
            const tempList = JSON.parse(JSON.stringify(list))
            const lists = tempList.map(e => {
                return {
                    ...e,
                    stationAreaName: `${e.provinceName}${e.urbanName}${e.areaName}`,
                    delFlagName: e.delFlag == 1 ? '启用' : '停用'
                }
            })
            jsonParse.sendResult(res, 200, {
                total,
                records: lists
            }, '查询成功')
        })
    },

    /**
     * 场站列表
     * @param {*} req 
     * @param {*} res 
     */
    async stationLists(req, res) {
        CarStation.find({delFlag: '1'}, {
            stationName: 1,
        }).then(list => {
            jsonParse.sendResult(res, 200, list, '查询成功')
        })
    },
    
    /**
     * 新增场站
     * @param {*} req 
     * @param {*} res 
     */
    async createStation(req, res) {
        const newCarStation = new CarStation({
            ...req.body,
        })
        newCarStation.save().then(station => {
            jsonParse.sendResult(res, 200, '场站创建成功')
        })
    },

    /**
     * 更新场站
     * @param {*} req 
     * @param {*} res 
     */
    async createStation(req, res) {
        const newCarStation = new CarStation({
            ...req.body,
        })
        newCarStation.save().then(station => {
            jsonParse.sendResult(res, 200, '场站创建成功')
        })
    },
}